﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Data;
using OF.Common;
using System.Configuration;
using System.Drawing;
using System.IO;
namespace OF.BLL.Dish
{
    /// <summary>
    /// 区域、商家接口实现
    /// </summary>
    public class Dish
    {

        DAL.DishCarts dal_cat = new DAL.DishCarts();
        DAL.Dish dal_dish = new DAL.Dish();

        #region 根据商家获取菜类和菜

        public string doGetCatDish(string sellerid)
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet dscat = dal_cat.GetList("sellerid=" + sellerid);
                DataSet dsdish = dal_dish.GetList("sellerid=" + sellerid);
                DataTable dtcat = dscat.Tables[0];
                DataTable dtdish = dsdish.Tables[0];
                string json = "{\"CatDish\":[";
                if (dscat != null && dscat.Tables[0] != null && dscat.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < dtcat.Rows.Count; i++)
                    {
                        json += "{\"catid\":\"" + dtcat.Rows[i]["id"] + "\",\"catname\":\"" + dtcat.Rows[i]["catname"] + "\",";
                        string dish = "[";
                        string catid = dtcat.Rows[i]["id"].ToString();
                        for (int j = 0; j < dtdish.Rows.Count; j++)
                        {
                            if (dtdish.Rows[j]["catid"].ToString() == catid)
                            {
                                dish += "{\"dishid\":\"" + dtdish.Rows[j]["id"] + "\",\"dishname\":\"" + dtdish.Rows[j]["dishname"] + "\",\"photo\":\"" + dtdish.Rows[j]["photo"] + "\",\"hashot\":\"" + dtdish.Rows[j]["hashot"] + "\",\"hasnew\":\"" + dtdish.Rows[j]["hasnew"] + "\",\"price\":\"" + dtdish.Rows[j]["price"] + "\"},";
                            }
                        }
                        if (dish.Length > 1) dish = dish.Substring(0, dish.Length - 1);
                        dish += "]";
                        json += "\"dishs\":" + dish + "},";
                    }
                    json = json.Substring(0, json.Length - 1);
                }
                json += "]}";
                result = json;
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetCatDish");
            }
            return result;
        }

        #endregion

        #region 根据商家获取类别 dish.catsbyseller.get
        public string doGetCatsBySeller(string sellerid)
        {
           string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_cat.GetList("sellerid=" + sellerid);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("SellerCategoryList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetCatsBySeller");
            }
            return result;
        }
        #endregion

        #region 根据类别获取菜品 dish.dishlistbycat.get
        public string doGetDishByCat(string catid)
        {
           string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_dish.GetList("catid=" + catid);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("CategoryDishList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetDishByCat");
            }
            return result;
        }
        #endregion

        #region 根据菜品获取菜品详细 dish.dishdetail.get
        public string doGetDishDetail(string dishid)
        {
           string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_dish.GetList("id=" + dishid);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("DishDetail", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetDishDetail");
            }
            return result;
        }
        #endregion

        #region 添加菜品dish.add

        public string addupdateDish(string dishname, string price, string catid, string photo, string sellerid,string state, string dishid, string type)
        {
            bool r1 = false;
            int r2 = 0;
            int _state = Common.TypeConverter.StrToInt(state);
            Model.Dish modelDish = new Model.Dish();
            if (type == "update") modelDish = dal_dish.GetModel(Common.TypeConverter.StrToInt(dishid));
            
            modelDish.catid = Convert.ToInt32(catid);
            modelDish.dishname = dishname;
            modelDish.price = Convert.ToDecimal(price);
            modelDish.sellerid = Convert.ToInt32(sellerid);
            modelDish.state = _state;

            if (type == "update")
            {
                r1 = dal_dish.Update(modelDish);
            }
            else if (type == "add")
            {
                modelDish.ordertimes = 0;
                modelDish.addtime = DateTime.Now;
                r2 = dal_dish.Add(modelDish);
            }

            if (r1 == true || r2 > 0)
            {
                return Enmus.RETURN.SUC.ToString();
            }
            else
            {
                return Enmus.RETURN.ERROR.ToString();
            }
        }

        #endregion

        #region 根据商家获取所有菜

        public string getAllDishbySeller(string sellerid,string state)
        {
            string result = Enmus.RETURN.NULL.ToString();
            string where = "sellerid=" + sellerid;
            if (state == "shangjia")
            {
                where += " and state=1 ";
            }
            else if(state=="xiajia")
            {
                where += " and state=0 ";
            }
            DataSet ds = dal_dish.GetList(where);
            try
            {
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("AllSellerDish", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "getAllDishbySeller");
            }
            return result;
        }

        #endregion

        #region 通过菜品id修改状态

        public string modifyDishState(string dishid)
        {
            string result = Enmus.RETURN.NULL.ToString();
            Model.Dish modelDish= dal_dish.GetModel(Common.Utils.StrToInt(dishid,0));
            if (modelDish != null)
            {
                int state = (int)modelDish.state;
                if (state == 1)
                {
                    modelDish.state = 0;
                }
                else if (state == 0)
                {
                    modelDish.state = 1;
                }
                bool r= dal_dish.Update(modelDish);
                if (r == true)
                {
                    result = Enmus.RETURN.SUC.ToString();
                }
            }
            return result;
        }
        #endregion
    }
}
